package com.lvhengchang.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lvhengchang.pojo.Card;
import com.lvhengchang.pojo.Result;
import com.lvhengchang.pojo.User;
import com.lvhengchang.service.CardService;
import com.lvhengchang.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	// @Resource
	private UserService userService;
	@Autowired
	private CardService cardService;

	/**
	 * 查询所有用户信息
	 */
	@GetMapping("/select")
	public Result<?> selectInfo() {
		List<User> users = userService.selectUserList();
		if (users != null) {
			return Result.success(users, "selectall user success");
		} else {
			return Result.fail(500, "selectall user fail");
		}
	}

//	@GetMapping("/selectid/{id}/{name}")
//	public User selectById(@PathVariable int id, @PathVariable("name") String username) {
//		System.out.println("username is" + username);
//		return userService.getUserById(id);
//	}
	@GetMapping("/selectid/{id}/{name}")
	public Result<?> selectById(@PathVariable int id, @PathVariable("name") String username) {
		System.out.println("username is" + username);
		User user = userService.getUserById(id);
		if (user != null) {
			return Result.success(user, "select user success");
		} else {
			return Result.fail(500, "select user fail");
		}
	}

	/**
	 * 插入用户信息
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping("/insert")
	public Result<?> insertInfo(@RequestBody User user) throws Exception {
		int result = userService.insertUser(user);
		int userid = user.getUserId();
		Card card = new Card();
		card.setBalance("0");
		card.setBelong(userid);
		int newresult = cardService.insertCard(card);
		if (result != 0) {
			return Result.success(result, "insert user success");
		} else {
			return Result.fail(500, "insert user fail");
		}
	}

	/**
	 * 删除用户信息,同时删除该用户的银行账户
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/")
	public Result<?> deleteInfo(int id) {
		int result = userService.deleteUser(id);
		cardService.deleteCardByBelong(id);
		if (result > 0) {
			return Result.success(result, "删除用户及银行账户成功");
		} else {
			return Result.fail(500, "删除用户及银行账户失败");
		}
	}

	/**
	 * 删除用户信息,保留用户银行账户
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/deleteuseronly")
	public Result<?> deleteInfoUserOnly(int id) {
		int result = userService.deleteUser(id);
		if (result > 0) {
			return Result.success(result, "删除用户成功,银行账户未删除");
		} else {
			return Result.fail(500, "删除用户失败");
		}
	}

	/**
	 * 修改用户信息
	 * 
	 * @param user
	 * @return
	 */
	@PutMapping("/update")
	public Result<?> updateInfo(@RequestBody User user) {
		int result = userService.updateById(user);
		if (result > 0) {
			return Result.success(result, "update user success");
		} else {
			return Result.fail(500, "update user fail");
		}
	}
}
